<?php

namespace app\dao;

use app\model\ProjectModel;
use think\facade\Log;
use think\facade\Db;

class ProjectDao extends BaseDao
{

    private $query;


    protected function setModel()
    {
        return ProjectModel::class;
    }

    public function __construct()
    {
        $this->query = new ProjectModel();
    }
    // 创建竞赛项目
    public static function createProject($name, $description, $adminId)
    {

        try {
            return ProjectModel::create([
                'name' => $name,
                'description' => $description,
                'admin_id' => $adminId
            ]);
        } catch (\Exception $e) {
            Log::error("创建竞赛项目失败: " . $e->getMessage());
            return null;
        }
    }

    // 为项目添加指导老师
    public static function addTeacher($projectId, $teacherId)
    {
        try {
            return Db::name('project_teachers')->insert([
                'project_id' => $projectId,
                'teacher_id' => $teacherId
            ]);
        } catch (\Exception $e) {
            Log::error("为项目添加指导老师失败: " . $e->getMessage());
            return false;
        }
    }

    // 根据项目 ID 获取项目
    public static function getProjectById($projectId)
    {
        try {
            return ProjectModel::find($projectId);
        } catch (\Exception $e) {
            Log::error("根据项目ID获取项目失败: " . $e->getMessage());
            return null;
        }
    }

    // 获取所有项目
    public static function getAllProjects()
    {
        try {
            return ProjectModel::select();
        } catch (\Exception $e) {
            Log::error("获取所有项目失败: " . $e->getMessage());
            return [];
        }
    }

    // 更新项目
    public static function updateProject($projectId, array $data)
    {
        try {
            return ProjectModel::where('id', $projectId)->update($data);
        } catch (\Exception $e) {
            Log::error("更新项目失败: " . $e->getMessage());
            return false;
        }
    }

    // 删除项目
    public static function deleteProject($projectId)
    {
        try {
            return ProjectModel::where('id', $projectId)->delete();
        } catch (\Exception $e) {
            Log::error("删除项目失败: " . $e->getMessage());
            return false;
        }
    }
}
